package com.example.restatr.Atcivity.CitySubway;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.restatr.Atcivity.Main.LoginAt;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.Main.Fragment.UserFragment;
import com.example.restatr.R;
import com.example.restatr.Tools;

import org.json.JSONException;
import org.json.JSONObject;

public class MetroCardAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private TextView cardNumber;
    private TextView cardTime;
    private ImageView zsIcon;
    private TextView zsText;
    private LinearLayout zsLl;
    private RelativeLayout card;
    private SharedPreferences sp;
    private SharedPreferences.Editor edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_ddk_at );

        sp = getSharedPreferences( "ddkInfo", MODE_PRIVATE );
        edit = sp.edit();
        tb = findViewById( R.id.toolbar);
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        cardNumber = findViewById( R.id.card_number );
        cardTime = findViewById( R.id.card_time );
        zsIcon = findViewById( R.id.zs_icon );
        zsText = findViewById( R.id.zs_text );
        zsLl = findViewById( R.id.zs_ll );
        card = findViewById( R.id.card );
        if (sp.getInt( "ddkCode", 0 ) == 200) {
            zsIcon.setImageResource( R.drawable.ic_baseline_remove_24 );
            zsText.setText( "注销乘车卡" );
            card.setVisibility( View.VISIBLE );
            cardTime.setText( sp.getString( "createTime", "" ) );
            cardNumber.setText( sp.getString( "cardNum", "" ) );
        }

        Tools.SetBarTitle( tb,barTitle,"乘车卡",search, MetroCardAt.this,false );

        zsLl.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MainActivity.sp.getInt( "code", 0 ) == 200 | UserFragment.code == 200) {
                    if (sp.getInt( "ddkCode", 0 ) == 200) {

                        //注销乘车卡
                        new Thread( new Runnable() {
                            @Override
                            public void run() {
                                final String delete = Tools.Delete( "/prod-api/api/metro/card/" +
                                                sp.getString( "id", null ),
                                        MainActivity.sp.getString( "token", null ) );
                                if (delete != null) {
                                    runOnUiThread( new Runnable() {
                                        @Override
                                        public void run() {
                                            try {
                                                JSONObject jx = new JSONObject( delete );
                                                Toast.makeText( MetroCardAt.this, jx.getString( "msg" ), Toast.LENGTH_SHORT ).show();
                                                int code = jx.getInt( "code" );
                                                if (code == 200) {
                                                    edit.putInt( "ddkCode", 0 ).commit();
                                                    card.setVisibility( View.GONE );
                                                    zsIcon.setImageResource( R.drawable.ic_round_add_24 );
                                                    zsText.setText( "领取乘车卡" );
                                                }
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    } );
                                }
                            }
                        } ).start();
                    } else {
                        //创建乘车卡
                        new Thread( new Runnable() {
                            @Override
                            public void run() {
                                JSONObject jsonObject = new JSONObject();
                                //获取用户信息
                                final String get = Tools.Get( "/prod-api/api/common/user/getInfo", MainActivity.sp.getString( "token", null ) );
                                if (get != null) {
                                    try {
                                        JSONObject jx = new JSONObject( get );
                                        int code = jx.getInt( "code" );
                                        if (code == 200) {
                                            JSONObject user = jx.getJSONObject( "user" );
                                            jsonObject.put( "userName", user.getString( "userName" ) );
                                            jsonObject.put( "phonenumber", user.getString( "phonenumber" ) );
                                            jsonObject.put( "idCard", user.getString( "idCard" ) );
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                                //领取乘车卡
                                final String token = Tools.Post( "/prod-api/api/metro/card", MainActivity.sp.getString( "token", null ), jsonObject.toString() );
                                if (token != null) {
                                    runOnUiThread( new Runnable() {
                                        @Override
                                        public void run() {
                                            try {
                                                JSONObject jx = new JSONObject( token );
                                                int code = jx.getInt( "code" );
                                                Toast.makeText( MetroCardAt.this, jx.getString( "msg" ), Toast.LENGTH_SHORT ).show();
                                                if (code == 200) {
                                                    edit.putInt( "ddkCode", code ).commit();
                                                    card.setVisibility( View.VISIBLE );
                                                    zsIcon.setImageResource( R.drawable.ic_baseline_remove_24 );
                                                    zsText.setText( "注销乘车卡" );
                                                    getCardInfo();
                                                }
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    } );
                                }

                            }
                        } ).start();
                    }
                } else {
                    startActivity( new Intent( MetroCardAt.this, LoginAt.class ) );
                }

            }
        } );

    }

    private void getCardInfo() {
        //获取乘车卡信息
        if (sp.getInt( "ddkCode", 0 ) == 200) {
            new Thread( new Runnable() {
                @Override
                public void run() {
                    final String info = Tools.Get( "/prod-api/api/metro/card", MainActivity.sp.getString( "token", "" ) );
                    if (info != null) {
                        runOnUiThread( new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    JSONObject jx = new JSONObject( info );
                                    int code = jx.getInt( "code" );
                                    if (code == 200) {
                                        JSONObject data = jx.getJSONObject( "data" );
                                        cardTime.setText( data.getString( "createTime" ) );
                                        cardNumber.setText( data.getString( "cardNum" ) );
                                        edit.putString( "createTime", data.getString( "createTime" ) );
                                        edit.putString( "cardNum", data.getString( "cardNum" ) );
                                        edit.putString( "id", data.getString( "id" ) );
                                        edit.commit();
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        } );
                    }
                }
            } ).start();


        }
    }
}